#include "App_At_Command.h"
#include "App_Lora_Task.h"
#include "App_Mac.h"
#include "board.h"
#include "log.h"
#include "system.h"

void App_Lora_Task(void* pvParameters)
{
    LoRaMacPrimitives_t LoRaMacPrimitives;
    LoRaMacCallback_t LoRaMacCallbacks;
    DeviceState = DEVICE_STATE_INIT;
    while (1) {
        WatchDogToggle();
        vTaskDelay(APP_LORA_TASK_DELAY_MS);
        App_Service_At_Command_Process();
        Radio.IrqProcess();

        switch (DeviceState) {
            case DEVICE_STATE_INIT: {  // 设备初始化，初始化相关的LORaMAC层的相关回调函数和参数
                AppMacInit(&LoRaMacPrimitives, &LoRaMacCallbacks);
                DeviceState = DEVICE_STATE_JOIN;
                break;
            }
            case DEVICE_STATE_JOIN: {
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_JOIN\r\n");
                AppMacJoin();
                DeviceState = DEVICE_STATE_IDLE;
                break;
            }
            case DEVICE_STATE_TIME_CHECK: {
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_TIME_CHECK\r\n");
                if (AppMacDeviceTimeCheck() == true) {
                    DeviceState = DEVICE_STATE_SEND;
                } else {
                    // DeviceState = DEVICE_STATE_IDLE;
                }
                break;
            }
            case DEVICE_STATE_SEND: {
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_SEND\r\n");
                // AppMacSendData();
                if (AppMacStateCheck()) {
                    SendFrame();
                    DeviceState = DEVICE_STATE_IDLE;
                } else {
                    DeviceState = DEVICE_STATE_JOIN;
                }

                break;
            }
            case DEVICE_STATE_CYCLE: {
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_CYCLE\r\n");
                DeviceState = DEVICE_STATE_IDLE;
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_CYCLE,time = %lu\r\n", SystemParmData.txDutyCycleTime * 1000);
                // Schedule next packet transmission
                TimerSetValue((void*)&(SystemLoRaData.txNextPacketTimer), SystemParmData.txDutyCycleTime * 1000);
                TimerStart((void*)&(SystemLoRaData.txNextPacketTimer));
                break;
            }
            case DEVICE_STATE_SLEEP: {  // 设备进入低功耗模式
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_CYCLE\r\n");
                DeviceState = DEVICE_STATE_SLEEP;
                Radio.IrqProcess();
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_SLEEP,txDutyCycleTime = %lu\r\n",
                           SystemParmData.txDutyCycleTime * 1000);
                if (SystemParmData.txDutyCycleTime * 1000 > 0) {
                    // Wake up through events
                    TimerLowPowerHandler();
                } else  // 直接休眠，需要外部管脚复位
                {
                    LOG_PRINTF(LL_DEBUG, "SystemEnterSleep\r\n");
                    SystemEnterSleep();
                }
                LOG_PRINTF(LL_DEBUG, "DEVICE_STATE_SLEEP,system weakup,DeviceState=%d\r\n", DeviceState);
                break;
            }
            default: {
                DeviceState = DEVICE_STATE_IDLE;
                if (SystemLoRaData.systemState.flagBit.loraJoinSuccess != 1) {
                    if (SystemLoRaData.systemState.flagBit.loraJoin != 1) {
                        DeviceState = DEVICE_STATE_JOIN;
                    }

                } else if (SystemLoRaData.systemState.flagBit.loraDeviceTime != 1) {
                    if (SystemLoRaData.systemState.flagBit.loraDeviceTiming != 1) {
                        DeviceState = DEVICE_STATE_TIME_CHECK;
                    }
                } else {
                    // DeviceState = DEVICE_STATE_SEND;
                }
                break;
            }
        }
    }
}
